#include"apue.h"
#include<fstream>
using namespace std;
using namespace MC;

Servicedao_file_impl::Servicedao_file_impl(){}
void Servicedao_file_impl::load(vector<Department>& dp)
{
	ifstream in;
	in.open("./account/service.dat",ios::binary);
	vector<Employee> vecEmps;
	Employee e;
	char name[20];
	int id;
	unsigned int count;
	//Department d(vecEmps,"none",0);
	//int ret;
	while(in.read((char*)&id,sizeof(id)))
	{
		
		in.read((char*)&name,sizeof(name));
		
		in.read((char*)&count,sizeof(count));
		for(unsigned int i=0;i<count;i++)
		{
			in.read((char*)&e,sizeof(e));
			vecEmps.push_back(e);
		}
		Department d(vecEmps,name,id);
		dp.push_back(d);
	}
	
	
	
	in.close();
}


void Servicedao_file_impl::save(vector<Department>& dp)
{
	ofstream out;
	out.open("./account/service.dat",ios::binary);
	//vector<Department>:: iterator p=dp.begin();
	for(unsigned int i=0;i<dp[i].m_vecEmps.size();i++)
	{
		out.write((char*)&(dp.at(i).m_nid),sizeof(int));
		out.write((char*)&(dp.at(i).m_strName),sizeof(dp.at(i).m_strName));
		unsigned int count=dp.at(i).m_vecEmps.size();
		out.write((char*)&count,sizeof(count));
		for(unsigned int j=0;j<count;j++)
		{
			//unsigned int count=dp.at(i).m_vecEmps.size();
			//unsigned int count=dp.at(i).m_vecEmps.size();
			out.write((char*)&(dp.at(i).m_vecEmps.at(j)),sizeof(Employee));
		}
	}
	out.close();

}